<?php
// This file is part of Meta AGM grading method - https://code.google.com/p/oasychev-moodle-plugins
//
// Meta AGM grading method is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Meta AGM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Meta AGM.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Meta AGM manager page.
 *
 * @package    gradingform_metaagm
 * @author     Nikita Kalinin <nixorv@gmail.com>
 * @copyright  2014 Oleg Sychev (Volgograd State Technical University) <oasychev@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');
require_once($CFG->dirroot.'/grade/grading/lib.php');

$areaid = required_param('areaid', PARAM_INT);
$action = optional_param('action', 'list', PARAM_ACTION);

/*
 * Possible actions:
 * list   - show defined AGMs (metacriterions) related to current meta AGM,
 * add    - create a new area for the metacriterion.
 * editmc - edit metacriterion settings.
 */

$manager = get_grading_manager($areaid);

list($context, $course, $cm) = get_context_info_array($manager->get_context()->id);

require_login($course, true, $cm);
require_capability('moodle/grade:managegradingforms', $context);

$controller = $manager->get_controller('metaagm');

$PAGE->set_url(new moodle_url('/grade/grading/form/metaagm/edit.php', array('areaid' => $areaid)));
$PAGE->set_title(get_string('manager', 'gradingform_metaagm'));
$PAGE->set_heading(get_string('manager', 'gradingform_metaagm'));

$returnurl = optional_param('returnurl', $manager->get_management_url(), PARAM_LOCALURL);

if ($action == 'list') {
    $controller->check_update_metaagm();
    $mcs = $controller->get_metacriteria();
    
    $mcforms = array();
    foreach ($mcs as $mc) {
        $mcforms[] = new \gradingform_metaagm\view\form_metacriterion(new moodle_url($PAGE->url), array('metacriterion' => $mc));
    }
    
    foreach ($mcforms as $form) {
        if ($data = $form->get_data()) {
            if (isset($data->newdef) || isset($data->editform)) {
                // Create new definition.
                $mcman = get_grading_manager($mcs[$data->metacriterionid]->subareaid);
                $mccntrl = $mcman->get_controller($mcs[$data->metacriterionid]->activemethod);
                redirect($mccntrl->get_editor_url($PAGE->url));
                break; // Submitted form found. No more iterations needed.

            } else if (isset($data->deftmpl)) {
                $mcman = get_grading_manager($mcs[$data->metacriterionid]->subareaid);
                $mccntrl = $mcman->get_controller($mcs[$data->metacriterionid]->activemethod);
                $pickurl = new moodle_url('/grade/grading/pick.php', array('targetid' => $mccntrl->get_areaid()));
                redirect($pickurl);
                break; // Submitted form found. No more iterations needed.

            } else if (isset($data->rm)) {
                // Delete metacriterion and all it's grading forms.
                $controller->delete_metacriterion($mcs[$data->metacriterionid]);
                redirect($PAGE->url, get_string('metacriteriondeleted', 'gradingform_metaagm'), 5);
                break; // Submitted form found. No more iterations needed.
                
            } else if (isset($data->editmc)) {
                $action = 'editmc';
                break; // Submitted form found. No more iterations needed.

            } else if (isset($data->publishform)) {
                $mcman = get_grading_manager($mcs[$data->metacriterionid]->subareaid);
                $mccntrl = $mcman->get_active_controller();
                $definition = $mccntrl->get_definition();
                redirect(new moodle_url('/grade/grading/manage.php', array(
                    'areaid' => $mccntrl->get_areaid(),
                    'shareform' => $definition->id,
                    'returnurl' => $PAGE->url->out(false)
                )));
                break; // Submitted form found. No more iterations needed.

            } else if (isset($data->rmform)) {
                $mcman = get_grading_manager($mcs[$data->metacriterionid]->subareaid);
                $mccntrl = $mcman->get_active_controller();
                $mccntrl->delete_definition();
                redirect($PAGE->url, get_string('manageactiondeletedone', 'core_grading'), 5);
                break; // Submitted form found. No more iterations needed.
            }
        }
    }

} else if ($action == 'add') {
    $methods = $manager->get_available_methods(false);
    $settingsform = new \gradingform_metaagm\view\form_metacriterion_settings(
            new moodle_url($PAGE->url, array('areaid' => $areaid, 'action' => 'add')),
            array('methods' => $methods)
    );
    
    if ($settingsform->is_cancelled()) {
        redirect($PAGE->url);
    } else if ($data = $settingsform->get_data()) {
        $controller->add_metacriterion($data->name, $data->method, $data->weight);
        redirect($PAGE->url);
    }
    
    $settingsform->set_data(array('areaid' => $areaid));

}

if ($action == 'editmc') {
    $mcid = required_param('metacriterionid', PARAM_INT);
    $mc = $controller->get_metacriterion($mcid);
    $methods = $manager->get_available_methods(false);
    $settingsform = new \gradingform_metaagm\view\form_metacriterion_settings(
            new moodle_url($PAGE->url, array('areaid' => $areaid, 'action' => 'editmc', 'metacriterionid' => $mcid)),
            array('methods' => $methods)
    );
    
    if ($settingsform->is_cancelled()) {
        redirect($PAGE->url);
    } else if ($data = $settingsform->get_data()) {
        $mc->name         = $data->name;
        $mc->weight       = $data->weight;
        $mc->activemethod = $data->method;
        $controller->update_metacriterion($mc);
        redirect($PAGE->url);
    }
    
    $mc->method = $mc->activemethod;
    
    $settingsform->set_data($mc);
}

echo $OUTPUT->header();

if ($action == 'list') {
    $addbutton = new single_button(
        new moodle_url($PAGE->url, array('areaid' => $areaid, 'action' => 'add')),
        get_string('addmetacriterion', 'gradingform_metaagm'),
        'get'
    );

    echo $OUTPUT->render($addbutton);
    
    if (empty($mcs)) {
        echo html_writer::tag('h3', get_string('nometacriteria', 'gradingform_metaagm'));
        echo get_string('nometacriteriahint', 'gradingform_metaagm');
    }
    
    foreach ($mcforms as $form) {
        $form->display();
    }

    if (!empty($mcs)) {
        // Render 'add' button at the end of page again for convenience.
        echo $OUTPUT->render($addbutton);
    }

} else if ($action == 'add') {
    echo html_writer::tag('h2', get_string('addmetacriterion', 'gradingform_metaagm'));
    $settingsform->display();

} else if ($action == 'editmc') {
    echo html_writer::tag('h2', get_string('settingsa', 'gradingform_metaagm', $mc->name));
    $settingsform->display();
}

echo $OUTPUT->footer();
